package GUIs;

import Configs.Config;
import Controllers.NhaTroController;
import models.NhaTro;

public class f_nhanvien_chunhatro_nhatro_sua extends javax.swing.JFrame {

    private static f_nhanvien_chunhatro_nhatro_sua _instance; //Kiểm tra sự tồn tại của 2 instance trong cùng 1 form
    private final Config conn;
    private NhaTroController nhaTroControl;
    public NhaTro nhaTroSua;
    private String msnv;

    public f_nhanvien_chunhatro_nhatro_sua(Config conn) {
        this.conn = conn;
        initComponents();
    }

    public static f_nhanvien_chunhatro_nhatro_sua getInstance(Config conn,String msnv, String soGPDKKD) {
        if (_instance == null) {
            _instance = new f_nhanvien_chunhatro_nhatro_sua(conn);
        }
        _instance.msnv = msnv;
        _instance.nhaTroControl = new NhaTroController(_instance.conn);
        _instance.nhaTroSua = _instance.nhaTroControl.getNhaTroWithGPKD(soGPDKKD);
        _instance.resetForm();
        return _instance;
    }

    public void createAndShowUI() {
        setVisible(true);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnthemnhatro = new javax.swing.JPanel();
        lbtieude = new javax.swing.JLabel();
        lbsogpkd = new javax.swing.JLabel();
        tfsogpdk = new javax.swing.JTextField();
        tftennhatro = new javax.swing.JTextField();
        lbmota = new javax.swing.JLabel();
        lbsdt = new javax.swing.JLabel();
        lbdiachi = new javax.swing.JLabel();
        tfsdt = new javax.swing.JTextField();
        btnsua = new javax.swing.JButton();
        btnlammoi = new javax.swing.JButton();
        btnthoat = new javax.swing.JButton();
        lbtennhatro = new javax.swing.JLabel();
        scpmota = new javax.swing.JScrollPane();
        tamota = new javax.swing.JTextArea();
        sepdiachi = new javax.swing.JSeparator();
        lbsonha = new javax.swing.JLabel();
        lbhem = new javax.swing.JLabel();
        lbduong = new javax.swing.JLabel();
        lbquanhuyen = new javax.swing.JLabel();
        lbtinh = new javax.swing.JLabel();
        tfsonha = new javax.swing.JTextField();
        tfhem = new javax.swing.JTextField();
        tfduong = new javax.swing.JTextField();
        tfquanhuyen = new javax.swing.JTextField();
        tftinh = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Sửa thông tin nhà trọ chủ nhà trọ");
        setResizable(false);
        setType(java.awt.Window.Type.UTILITY);

        lbtieude.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        lbtieude.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        lbtieude.setText("Sửa thông tin Nhà trọ");

        lbsogpkd.setText("Số GPKD:");

        tfsogpdk.setEditable(false);

        lbmota.setText("Mô tả:");

        lbsdt.setText("SĐT:");

        lbdiachi.setText("Địa chỉ:");

        btnsua.setText("Sửa");
        btnsua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnsuaActionPerformed(evt);
            }
        });

        btnlammoi.setText("Làm mới");
        btnlammoi.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnlammoiActionPerformed(evt);
            }
        });

        btnthoat.setText("Thoát");
        btnthoat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnthoatActionPerformed(evt);
            }
        });

        lbtennhatro.setText("Tên nhà trọ:");

        tamota.setColumns(5);
        tamota.setLineWrap(true);
        tamota.setRows(5);
        scpmota.setViewportView(tamota);

        lbsonha.setText("Số nhà:");

        lbhem.setText("Hẻm:");

        lbduong.setText("Đường:");

        lbquanhuyen.setText("Quận Huyện:");

        lbtinh.setText("Tỉnh:");

        javax.swing.GroupLayout pnthemnhatroLayout = new javax.swing.GroupLayout(pnthemnhatro);
        pnthemnhatro.setLayout(pnthemnhatroLayout);
        pnthemnhatroLayout.setHorizontalGroup(
            pnthemnhatroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(lbtieude, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
            .addGroup(pnthemnhatroLayout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addGroup(pnthemnhatroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnthemnhatroLayout.createSequentialGroup()
                        .addGap(21, 21, 21)
                        .addGroup(pnthemnhatroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbsonha)
                            .addComponent(lbhem)
                            .addComponent(lbduong)
                            .addComponent(lbquanhuyen)
                            .addComponent(lbtinh))
                        .addGap(55, 55, 55)
                        .addGroup(pnthemnhatroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(tfsonha)
                            .addComponent(tfhem)
                            .addComponent(tfduong)
                            .addComponent(tfquanhuyen)
                            .addComponent(tftinh, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(sepdiachi, javax.swing.GroupLayout.PREFERRED_SIZE, 316, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(pnthemnhatroLayout.createSequentialGroup()
                        .addGroup(pnthemnhatroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lbsogpkd)
                            .addComponent(lbtennhatro)
                            .addComponent(lbsdt)
                            .addComponent(lbdiachi)
                            .addComponent(lbmota))
                        .addGap(35, 35, 35)
                        .addGroup(pnthemnhatroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(tftennhatro)
                            .addComponent(tfsogpdk)
                            .addComponent(tfsdt)
                            .addComponent(scpmota, javax.swing.GroupLayout.DEFAULT_SIZE, 194, Short.MAX_VALUE)))
                    .addGroup(pnthemnhatroLayout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addComponent(btnsua, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnlammoi, javax.swing.GroupLayout.PREFERRED_SIZE, 94, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnthoat, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(47, 47, Short.MAX_VALUE))
        );
        pnthemnhatroLayout.setVerticalGroup(
            pnthemnhatroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnthemnhatroLayout.createSequentialGroup()
                .addComponent(lbtieude, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(pnthemnhatroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbsogpkd)
                    .addComponent(tfsogpdk, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(30, 30, 30)
                .addGroup(pnthemnhatroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbtennhatro)
                    .addComponent(tftennhatro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnthemnhatroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbmota)
                    .addComponent(scpmota, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(8, 8, 8)
                .addGroup(pnthemnhatroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfsdt, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbsdt))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(lbdiachi)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(sepdiachi, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGroup(pnthemnhatroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnthemnhatroLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(pnthemnhatroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnsua)
                            .addComponent(btnlammoi)
                            .addComponent(btnthoat)))
                    .addGroup(pnthemnhatroLayout.createSequentialGroup()
                        .addGroup(pnthemnhatroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tfsonha, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lbsonha))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(pnthemnhatroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tfhem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lbhem))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(pnthemnhatroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tfduong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lbduong))
                        .addGap(7, 7, 7)
                        .addGroup(pnthemnhatroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tfquanhuyen, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lbquanhuyen))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(pnthemnhatroLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(tftinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(lbtinh))
                        .addGap(0, 64, Short.MAX_VALUE)))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnthemnhatro, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(pnthemnhatro, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void btnthoatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnthoatActionPerformed
       this.dispose();
    }//GEN-LAST:event_btnthoatActionPerformed

    private void btnlammoiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnlammoiActionPerformed
     this.resetForm();
    }//GEN-LAST:event_btnlammoiActionPerformed

    private void btnsuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnsuaActionPerformed
      if(this.nhaTroControl.Edit(this))
      {
         f_nhanvien_chunhatro_qlnhatro qlnhatro = f_nhanvien_chunhatro_qlnhatro.getInstance(this.conn, this.msnv, this.nhaTroSua.getChunhatro().getCMND());
         qlnhatro.refreshDsNhaTro();
         this.dispose();
      }
    }//GEN-LAST:event_btnsuaActionPerformed

    private void resetForm()
    {
       this.tfsogpdk.setText(this.nhaTroSua.getGPKD());
       this.tftennhatro.requestFocus();
       this.tftennhatro.setText(this.nhaTroSua.getTennhatro());
       this.tamota.setText(this.nhaTroSua.getMotanhatro());
       this.tfsdt.setText(this.nhaTroSua.getSdt());
       this.tfsonha.setText(this.nhaTroSua.getDiachi().getSonha());
       this.tfhem.setText(this.nhaTroSua.getDiachi().getHem());
       this.tfduong.setText(this.nhaTroSua.getDiachi().getDuong());
       this.tfquanhuyen.setText(this.nhaTroSua.getDiachi().getQuanhuyen());
       this.tftinh.setText(this.nhaTroSua.getDiachi().getTinhtp());
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnlammoi;
    private javax.swing.JButton btnsua;
    private javax.swing.JButton btnthoat;
    private javax.swing.JLabel lbdiachi;
    private javax.swing.JLabel lbduong;
    private javax.swing.JLabel lbhem;
    private javax.swing.JLabel lbmota;
    private javax.swing.JLabel lbquanhuyen;
    private javax.swing.JLabel lbsdt;
    private javax.swing.JLabel lbsogpkd;
    private javax.swing.JLabel lbsonha;
    private javax.swing.JLabel lbtennhatro;
    private javax.swing.JLabel lbtieude;
    private javax.swing.JLabel lbtinh;
    private javax.swing.JPanel pnthemnhatro;
    private javax.swing.JScrollPane scpmota;
    private javax.swing.JSeparator sepdiachi;
    public javax.swing.JTextArea tamota;
    public javax.swing.JTextField tfduong;
    public javax.swing.JTextField tfhem;
    public javax.swing.JTextField tfquanhuyen;
    public javax.swing.JTextField tfsdt;
    public javax.swing.JTextField tfsogpdk;
    public javax.swing.JTextField tfsonha;
    public javax.swing.JTextField tftennhatro;
    public javax.swing.JTextField tftinh;
    // End of variables declaration//GEN-END:variables
}
